//
//  LBWEmoticonButton.swift
//  weibo
//
//  Created by Limonwei on 15/12/18.
//  Copyright © 2015年 heima. All rights reserved.
//
//每一个表情是一个按钮  抽取出来


import UIKit

class LBWEmoticonButton: UIButton {

    var emoticon: LBWEmoticon? {
        didSet{
            guard let emo = emoticon else {
                return
            }
            //先判断是否是emoji表情
            if !emo.isEmoji {
                //如果是图片表情,直接加载图片
                let image = UIImage(named: emo.fullPath!, inBundle: LBWEmoticonTools.shareTools.emoticonBundle,compatibleWithTraitCollection: nil)
                setImage(image, forState: .Normal)
                setTitle(nil, forState: .Normal)
            }else{
                //设置emoji表情
                setTitle((emo.code! as NSString).emoji(), forState: .Normal)
                setImage(nil, forState: .Normal)
                
            }

        }
    }
    
    //将传入的popView显示在当前按钮表情上
    func showPopView(popView: LBWEmoticonPopView){
        //求出按钮在屏幕上的位置
        let rect = self.convertRect(bounds, toView: nil)
        
        //设置位置
        popView.center.x = CGRectGetMidX(rect)
        popView.y = CGRectGetMaxY(rect) - popView.height
        
        //设置数据
        
        popView.emoticonButton.emoticon = emoticon
        
        let window = UIApplication.sharedApplication().windows.last!
        
        window.addSubview(popView)
        
    }

}


















